from datetime import datetime

from django.shortcuts import render
from django.http import HttpResponse
import  json
from django.http import JsonResponse
from django.shortcuts import redirect
def index(request):

    body =request.body.decode()

    body= json.loads(body)
    print(body,type(body))#json形式的字符串

    #################################33
    # print(request.META)#打印请求头
    # content= request.META['CONTENT_TYPE']
    # print(content)
    print(request.method)
    ####################
    return HttpResponse('测试项目逻辑')
# Create your views here.
#
def detail(request,category_id,book_id):
    #get函数
    # params= request.GET
    # # print(params)
    # #一键多值化 getlist方法
    # username=params['username']
    # pswd =params.get('password')
    # print(username,pswd)
    #
    # usr= params.getlist('username')
    # print(usr)

    #Post表单数据
    # data = request.POST
    # print(data)

    #Json 数据
    body = request.body
    print(body)
    return HttpResponse('detail')

def yuan(request):
    # data ='hahah'
    # return  HttpResponse(data,status=300)
    #####HttpResponse####
    data= {"name":"huangjingyuan"}
    return JsonResponse(data,safe=False)

def jump(request):
    return redirect("http://www.4399.com")

def cook(request):

    username =request.GET.get('username')
    respon = HttpResponse('set_cookie')
    respon.set_cookie('username',username)

    return respon
    pass

def getcook(request):
    cookkies= request.COOKIES

    username = cookkies.get('username')
    print(username)
    return  HttpResponse('get_cookies')
    pass

def set_session(request):

    print(request.COOKIES)
    user_id =554
    request.session['user_id']=user_id
    return HttpResponse('set_session')
    pass

def get_session(request):

    print(request.COOKIES)
    user_id=request.session['user_id']
    return HttpResponse('get_session')
    pass

"""

get是获取登录的页面
pst请求是验证登录
"""
from django.views import View
class LoginView(View):
    def get(self,request):
        return HttpResponse('get')
        pass

from django.contrib.auth.mixins import LoginRequiredMixin
class CenterView(LoginRequiredMixin,View):
    def get(self, request):
        return HttpResponse('个人中心展示')
        pass
    def post(self,request):
        return HttpResponse('个人中心修改')

##########################################
class Homeview(View):
    def get(self,request):
        name = request.GET.get('username')
        print(name)

        context = {
            'username':name,
            'age':14,
            'first':datetime.now(),
            'second':['tom','jacl','rose''yuan'],
            'scores':{
                '2018':100,
                '2049':555,
                '2044':154,

            }
        }
        return render(request,'inex.html',context)